const local = require("../../../data/local");
const app = getApp();
Page({
  data: {
    // 音乐的播放状态
    isPlay: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var id = options.id;
    var data = local.postList[options.id];
    this.setData({
      data,
      postId: id,
      music: local.postList[options.id].music
    });
    this.onMusic();
    // 进入页面退出页面音乐播放按钮一致
    if (app.globalData.g_isPlay && app.globalData.g_currentId == id) {
      this.setData({
        isPlay: true
      });
    }
  },
  // 监听音乐播放
  onMusic() {
    var self = this;
    wx.onBackgroundAudioPlay((result) => {
      self.setData({
        isPlay: true
      });
      app.globalData.g_isPlay = true;
      app.globalData.g_currentId = self.data.postId;
    });
    wx.onBackgroundAudioPause((result) => {
      self.setData({
        isPlay: false
      })
      app.globalData.g_isPlay = false;
      app.globalData.g_currentId = null;
    });
    if (wx.stopBackgroundAudio()) {
      this.setData({
        isPlay: false
      })
    }
  },
  // 点击音乐播放
  playMusic() {
    var self = this;
    if (this.data.isPlay) {
      wx.stopBackgroundAudio();
      this.setData({
        isPlay: false
      });
    } else {
      wx.playBackgroundAudio({
        dataUrl: self.data.music.url,
        title: self.data.music.title,
        coverImgUrl: self.data.music.coverImgUrl
      });
      this.setData({
        isPlay: true
      });
    }
  }

})
